module.exports = {
    videoFunc: null,
    interstitialExist: false,
    rewardedVideoAd: null,
    interstitialAd: null,
    bannerAd: null,
    init() {
        console.log('广告初始化了')
        this.loadBanner()
        this.initVideo()
        this.loadInterstitialAd()
    },
    // 视频初始化
    initVideo() {
        this.rewardedVideoAd = uc.createRewardVideoAd();
        console.log(this.rewardedVideoAd)
        this.rewardedVideoAd.onLoad(() => {
            console.log('激励视频-广告加载成功');
        });

        this.rewardedVideoAd.onError(err => {
            console.log('激励视频-广告加载失败', err);
        });

        this.rewardedVideoAd.onClose(res => {
            // 用户点击了【关闭广告】按钮
            if (res && res.isEnded) {
                if (this.videoFunc) {
                    this.videoFunc()
                }
                console.log('正常播放结束，可以下发游戏奖励 res: ', res);
            } else {
                console.log('播放中途退出，不下发游戏奖励 res : ', res);
            }
        });
    },
    // 显示视频
    showVideo(fuc) {
        he.local.click("load_ad_click")
        this.videoFunc = fuc
        if (this.rewardedVideoAd) {
            this.rewardedVideoAd
                .show()
                .then()
                .catch(err => () => {
                    console.log(err)
                    this.initVideo()
                });
        } else {
            this.initVideo()
        }

    },

    // 加载Banner
    loadBanner() {
        let res = uc.getSystemInfoSync();
        console.log(res)
        if (typeof res === 'string') {
            try {
                res = JSON.parse(res);
            } catch (e) { }
        }

        let deviceWidth = res.screenWidth > res.screenHeight ? res.screenHeight : res.screenWidth;
        let width = deviceWidth / 2;
        let height = (width * 194) / 345;
        console.log(height)
        this.bannerAd = uc.createBannerAd({
            style: {
                width: 750,
                // height: 200,
                // bottom: 150,        //150
                gravity: 7,
            },
        });
        this.bannerAd.onError(err => {
            console.log('bannerAd 广告加载出错', err);
        });
        this.bannerAd.onLoad(() => {
            console.log('bannerAd 广告加载成功');
        });
    },
    //展示banner
    showBanner() {
        if (this.bannerAd) {
            this.isshowBanner = true
            this.bannerAd.show()
        } else {
            this.loadBanner()
        }
    },
    //关闭banner
    closeBanner() {
        if (this.bannerAd && this.isshowBanner) {
            this.isshowBanner = false
            this.bannerAd.hide()
        }
    },


    //加载插屏广告
    loadInterstitialAd() {
        this.interstitialAd = uc.createInterstitialAd();


        this.interstitialAd.onLoad(() => {
            console.log('插屏-广告加载成功');
        });

        this.interstitialAd.onError(err => {
            console.log('插屏-广告加载失败', err);
        });

        this.interstitialAd.onClose(res => {
            // 用户点击了【关闭广告】按钮
            console.log('插屏-关闭');
        });
    },
    //展示插屏广告
    showInterstitialAd() {
        if (this.interstitialAd) {
            this.interstitialAd
                .show()
                .then()
                .catch(err => () => {
                    this.loadInterstitialAd()
                });
        } else {
            this.loadInterstitialAd()
        }
    },
}